package com.workstudy.workstudy.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.workstudy.common.utils.ShiroUtils;
import com.workstudy.workstudy.domain.WorkstudyAttendance;
import com.workstudy.workstudy.domain.WorkstudyAttendanceWage;
import com.workstudy.workstudy.mapper.WorkstudyAttendanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.workstudy.workstudy.mapper.WorkstudyWageMapper;
import com.workstudy.workstudy.domain.WorkstudyWage;
import com.workstudy.workstudy.service.IWorkstudyWageService;
import com.workstudy.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工资Service业务层处理
 * 
 * @author ws
 * @date 2021-03-28
 */
@Service
public class WorkstudyWageServiceImpl implements IWorkstudyWageService 
{
    @Autowired
    private WorkstudyWageMapper workstudyWageMapper;

    @Autowired
    private WorkstudyAttendanceMapper workstudyAttendanceMapper;

    /**
     * 查询工资
     * 
     * @param wageId 工资ID
     * @return 工资
     */
    @Override
    public WorkstudyWage selectWorkstudyWageById(Long wageId)
    {
        return workstudyWageMapper.selectWorkstudyWageById(wageId);
    }

    /**
     * 查询工资列表
     * 
     * @param workstudyWage 工资
     * @return 工资
     */
    @Override
    public List<WorkstudyWage> selectWorkstudyWageList(WorkstudyWage workstudyWage)
    {
        return workstudyWageMapper.selectWorkstudyWageList(workstudyWage);
    }

    /**
     * 新增工资
     * 
     * @param workstudyWage 工资
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWorkstudyWage(WorkstudyWage workstudyWage,String attendanceIds)
    {
        //截取考勤id
        String[] split = attendanceIds.split(",");
        String attendanceId="";
        for (String s : split) {
            attendanceId+="'"+s+"',";
        }
        //结算人
        workstudyWage.setSettlementBy(ShiroUtils.getLoginName());
        //获取总工资
        String sumPrice = workstudyAttendanceMapper.selectWorkstudyAttendanceSumPrice(attendanceId.substring(0,attendanceId.length()-1));
        //获取总工时
        String sumDuration = workstudyAttendanceMapper.selectWorkstudyAttendanceSumDuration(attendanceId.substring(0,attendanceId.length()-1));
        workstudyWage.setSumprice(new BigDecimal(sumPrice));
        workstudyWage.setSumduration(new BigDecimal(sumDuration));
        int i = workstudyWageMapper.insertWorkstudyWage(workstudyWage);
        for (String s : split) {
            //新增考勤工资表
            WorkstudyAttendanceWage workstudyAttendanceWage=new WorkstudyAttendanceWage();
            workstudyAttendanceWage.setAttendanceId(Long.valueOf(s));
            workstudyAttendanceWage.setWageId(workstudyWage.getWageId());
            workstudyWageMapper.insertWorkstudyAttendanceWage(workstudyAttendanceWage);
            //修改考勤结算信息
            WorkstudyAttendance workstudyAttendance=new WorkstudyAttendance();
            //结算人
            workstudyAttendance.setSettlementBy(ShiroUtils.getLoginName());
            workstudyAttendance.setAttendanceId(Long.valueOf(s));
            workstudyAttendanceMapper.updateWorkstudyAttendanceStatus(workstudyAttendance);
        }
        return 1;
    }

    /**
     * 修改工资
     * 
     * @param workstudyWage 工资
     * @return 结果
     */
    @Override
    public int updateWorkstudyWage(WorkstudyWage workstudyWage)
    {
        return workstudyWageMapper.updateWorkstudyWage(workstudyWage);
    }

    /**
     * 删除工资对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWorkstudyWageByIds(String ids)
    {
        return workstudyWageMapper.deleteWorkstudyWageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工资信息
     * 
     * @param wageId 工资ID
     * @return 结果
     */
    @Override
    public int deleteWorkstudyWageById(Long wageId)
    {
        return workstudyWageMapper.deleteWorkstudyWageById(wageId);
    }

    @Override
    public List<String> selectBySumSumprice() {
        return workstudyWageMapper.selectBySumSumprice();
    }
}
